package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.common.BaseContext;
import com.project.common.R;
import com.project.entity.ShoppingCart;
import com.project.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        // 用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 菜品id
        Long dishId = shoppingCart.getDishId();

        // 创建条件对象
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        // 判断菜品或套餐
        if (dishId != null) {
            // 菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

        } else {
            // 套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 查询
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        // 判断是否存在于购物车
        if (cartServiceOne != null) {
            // 已存在，进行更新操作
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);

            shoppingCartService.updateById(cartServiceOne);
        } else {
            // 不存在，新增
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        // 用户id
        Long userId = BaseContext.getCurrentId();

        // 创建条件对象
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        // 查询
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);


        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        // 用户id
        Long userId = BaseContext.getCurrentId();

        // 创建条件对象
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        // 删除
        shoppingCartService.remove(queryWrapper);

        return R.success("清空成功");
    }

}
